using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Xml.Serialization;
using Microsoft.MediaCenter.UI;
using System.IO;
using System.Diagnostics;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;

namespace ClipShow2.MediaTypes
{
    public enum ContentSource { Local, WindowsHomeServer, Homegroup, SearchResults};

    #region IComparer classes
    public class MediaByDateReverseComparer : System.Collections.IComparer
    {
        public int Compare(object x, object y)
        {
            ClipShowMetadata firstInstance = x as ClipShowMetadata;
            ClipShowMetadata secondInstance = y as ClipShowMetadata;
            return secondInstance.CreationDateTime.CompareTo(firstInstance.CreationDateTime);
        }
    }
    public class MediaByDateComparer : System.Collections.IComparer
    {
        public int Compare(object x, object y)
        {
            ClipShowMetadata firstInstance = x as ClipShowMetadata;
            ClipShowMetadata secondInstance = y as ClipShowMetadata;
            return firstInstance.CreationDateTime.CompareTo(secondInstance.CreationDateTime);
        }
    }
    public class MediaByNameComparer : System.Collections.IComparer
    {
        public int Compare(object x, object y)
        {
            ClipShowMetadata firstInstance = x as ClipShowMetadata;
            ClipShowMetadata secondInstance = y as ClipShowMetadata;
            return firstInstance.Title.CompareTo(secondInstance.Title);
        }
    }
    #endregion

    [Serializable]
    public class ClipShowContent
    {
        public ClipShowContent()
        {
        }

        private ContentSource _contentSourceName = ContentSource.Local;
        private TimeSpan _contentDuration = new TimeSpan();

        [XmlAttribute]
        public ContentSource ContentSourceType
        {
            get { return (_contentSourceName);}
            set { _contentSourceName = value; }
        }

        [XmlElement]
        public TimeSpan ContentDuration
        {
            get { return (_contentDuration); }
            set { _contentDuration = value; }
        }
        [XmlElement(Type = typeof(ClipShowMetadata))]
        public ArrayList MediaItems = new ArrayList();

        public static object CloneObject(object obj)
        { using (MemoryStream memStream = new MemoryStream())
            { 
                BinaryFormatter binaryFormatter = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.Clone)); 
                binaryFormatter.Serialize(memStream, obj); 
                memStream.Seek(0, SeekOrigin.Begin); 
                return binaryFormatter.Deserialize(memStream); 
            }
        }
    }


    [Serializable]
    public class ClipShowMetadata
    {
        private string _sourceTitle;
        private string _sourceAuthor;
        private string _sourceDescription;
        private string _sourceUrl = "";
        private Image _thumbnail;
        private string _thumbnailUrl;
        private Int64 _sourceDuration;
        private DateTime _sourceDateTime;
        private bool _isPlaylistItem = false;
        private string _parentalRating;
        private bool _isGroupItem = false;
        private string _groupDescription = "";
        private int _groupCtr = 0;
        private Int64 _groupDuration = 0;
        private bool _isTempThumbnail = false;

        private string BuildImagePath(string filePath)
        {
            if (filePath.StartsWith("http"))
            {
                return (filePath);
            }
            else
            {
                if (File.Exists(filePath))
                {
                    return (String.Format("file://{0}", filePath));
                }
                else
                {
                    return ("");
                }
            }
        }

        [XmlIgnore]
        public bool IsThumbnailReady
        {
            get { return (File.Exists(this.ThumbnailUrl));}
        }
        [XmlIgnore]
        public bool IsTempThumbnail
        {
            get { return (_isTempThumbnail); }
            set { _isTempThumbnail = value;  }
        }
        [XmlIgnore]
        public Image GetGroupThumbnail
        {
            get
            {
                if (this.IsGroupItem)
                {
                    _groupCtr++;
                    if (_groupCtr > this.GroupItems.Count-1)
                    {
                        _groupCtr = 0;
                    }
                    ClipShowMetadata child = (ClipShowMetadata)this.GroupItems[_groupCtr];
                    string thumbNailPath = BuildImagePath(child.ThumbnailUrl);
                     
                    // Update the title/description to reflect the current media item
                    this.Title = child.Title;
                    this.Duration = child.Duration;
                    this.Description = String.Format("{0}\n({1} clips grouped by date)",
                        child.Description, this.GroupItems.Count);


                    if (thumbNailPath.Equals(""))
                    {
                        if (File.Exists(this.ThumbnailUrl))
                        {
                            return (new Image(BuildImagePath(this.ThumbnailUrl)));
                        }
                        else
                        {
                            string tempThumb = String.Format("{0}\\{1}_temp.jpg",
                                Path.GetDirectoryName(this.ThumbnailUrl), Path.GetFileNameWithoutExtension(this.ThumbnailUrl));
                            return (new Image(BuildImagePath(tempThumb)));
                        }
                    }
                    else
                    {
                        return (new Image(thumbNailPath));
                    }
                }
                else
                {
                    this.IsTempThumbnail = false;
                    return (new Image(BuildImagePath(this.ThumbnailUrl)));
                }
            }
        }
        [XmlElement(Type = typeof(ClipShowMetadata))]
        public ArrayList GroupItems = new ArrayList();

        [XmlIgnore]
        public bool IsPlaylistItem
        {
            get
            {
                return (_isPlaylistItem);
            }
            set
            {
                _isPlaylistItem = value;
            }
        }

        [XmlAttribute]
        public bool IsGroupItem
        {
            get { return (_isGroupItem); }
            set { _isGroupItem = value; }
        }

        [XmlAttribute]
        public string GroupItemDescription
        {
            get { return (_groupDescription); }
            set { _groupDescription = value; }
        }

        [XmlAttribute]
        public string ParentalRating
        {
            get { return (_parentalRating); }
            set { _parentalRating = value; }
        }
        [XmlAttribute]
        public Int64 Duration
        {
            get { return (_sourceDuration); }
            set { _sourceDuration = value; }
        }

        [XmlAttribute]
        public Int64 GroupDuration
        {
            get { return (_groupDuration); }
            set { _groupDuration = value; }
        }

        [XmlAttribute]
        public DateTime CreationDateTime
        {
            get { return (_sourceDateTime); }
            set { _sourceDateTime = value; }
        }

        [XmlAttribute]
        public string Url
        {
            get { return (_sourceUrl); }
            set { _sourceUrl = value; }
        }

        [XmlAttribute]
        public string Author
        {
            get { return (_sourceAuthor); }
            set { _sourceAuthor = value; }
        }

        [XmlAttribute]
        public string Description
        {
            get { return (_sourceDescription); }
            set { _sourceDescription = value; }
        }

        [XmlAttribute]
        public string Title
        {
            get { return (_sourceTitle); }
            set { _sourceTitle = value; }
        }

        [XmlIgnore]
        public Image Thumbnail
        {
            get { return (_thumbnail); }
            set { _thumbnail = value; }
        }

        [XmlAttribute]
        public string ThumbnailUrl
        {
            get { return (_thumbnailUrl); }
            set { _thumbnailUrl = value; }
        }
    }
}
